package com.shao.web;

import com.shao.pojo.Book;
import com.shao.pojo.Page;
import com.shao.service.BookService;
import com.shao.service.impl.BookServiceImpl;
import com.shao.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: SJP
 * Date: 2021/5/22
 * Time: 9:56
 * Description: No Description
 */
public class ClientBoookServlet extends BaseServlet{
    BookService bookService = new BookServiceImpl();
    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //获取请求参数当前页码pageNn和页显示数据数pageSize
        int pageNn = WebUtils.parseInt(req.getParameter("pageNn"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用BookService.page(pageNn，pageSize)：Page对象
        Page<Book> page = bookService.page(pageNn,pageSize);
        page.setUrl("client/bookServlet?action=page");
        //将请求参数保存到request域中
        req.setAttribute("page",page);
        //请求转发到pages/manager/book_manage.jsp
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNn,pageSize,Min Max
        //获取请求参数当前页码pageNn和页显示数据数pageSize
        int pageNn = WebUtils.parseInt(req.getParameter("pageNn"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int max = WebUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE);
        int min = WebUtils.parseInt(req.getParameter("min"), 0);
        //2 调用BookService.page(pageNn，pageSize)：Page对象
        Page<Book> page = bookService.pageByPrice(pageNn,pageSize,min,max);

        StringBuilder sb = new StringBuilder("client/bookServlet?action=pageByPrice");
        if(req.getParameter("min")!=null){
            sb.append("&min=").append(req.getParameter("min"));
        }
        if(req.getParameter("max")!=null){
            sb.append("&max=").append(req.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //将请求参数保存到request域中
        req.setAttribute("page",page);
        //请求转发到pages/manager/book_manage.jsp
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
 }
